//
//  HelpViewController.m
//  Light
//
//  Created by 骁唐 on 15/11/2.
//  Copyright © 2015年 骁唐. All rights reserved.
//

#import "HelpViewController.h"

@interface HelpViewController ()<UIGestureRecognizerDelegate,MBProgressHUDDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *backGround;//背景图
@property (weak,nonatomic) IBOutlet UITableView * tableView;//表格

@end

@implementation HelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark -
#pragma mark 导航返回
- (IBAction)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 界面销毁(禁用滑动手势)
- (void)viewDidAppear:(BOOL)animated {
    //禁用滑动手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

#pragma mark -
#pragma mark 界面将要展现（设置背景）
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    MBPROGRESS_ALLOC;
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
    long long int currentTimeInt = [TJJDefines getTimeIntSince1970Current];
    if (currentTimeInt>=16200&& currentTimeInt <=34200) {
        self.backGround.image = [UIImage imageNamed:@"backgroundOne"];
    }else if (currentTimeInt>34200 && currentTimeInt<=55800)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundTwo"];
    }else if (currentTimeInt>55800 && currentTimeInt<=70200)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundThree"];
    }else if(currentTimeInt >70200 || currentTimeInt<16200)
    {
        self.backGround.image = [UIImage imageNamed:@"background_night"];
    }
    [self getUserHelpInfo];//获取数据
}

#pragma mark -
#pragma mark 获取用户帮助信息
- (void)getUserHelpInfo {
    //b[新闻标题 newsTitle ,新闻内容 newsContent]
    //a [1成功    0其他 ]
    
    [_mbProgressHud show:YES];
    AFHTTPRequestOperationManager *httpManager = [AFHTTPRequestOperationManager manager];
    httpManager.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
    httpManager.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
    //传的参数
    UserConfig * config = [UserConfig sharingUserConfig];
    NSDictionary *dic= @{@"Token":[config getToken]};
    //地址
    [httpManager POST:USER_HELP parameters:dic
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  _mbProgressHud.hidden = YES;
                  NSDictionary *dic = [NetworkTool toNSDictionary:responseObject];
                  NSLog(@"获取成功");
                  //处理后台返回空字段
                  NSMutableString *string = [NSMutableString stringWithString:dic[@"b"]];
                  [string replaceOccurrencesOfString:@"null" withString:@"\"\"" options:NSLiteralSearch range:NSMakeRange(0, string.length)];
                  
                  NSDictionary *resDic = [NetworkTool parseJSONStringToNSDictionary:string];
                  //a[1成功    0其他 ]
                  if ([dic[@"a"] intValue] == 1)
                  {//成功
                      //b[新闻标题 newsTitle ,新闻内容 newsContent]
                      [self updateUI:resDic];//将获取到的内容，更新展示
                  }
                  else
                  {
                      TJJALERT_VIEW(@"其他错误");
                  }
              }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  _mbProgressHud.hidden = YES;
                  TJJALERT_VIEW(ERROR_NETWORKINFO)
              }];
    
}

#pragma mark -
#pragma mark 更新UI展示信息
- (void)updateUI:(NSDictionary *)dictionary
{
    //所有内容加载在headView上，最后将tableView的headView设置为指定的headView
    UILabel * labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, SCREEN_WIDTH-40, 40)];
    labelTitle.textColor = [UIColor whiteColor];//设置字体颜色
    [labelTitle setFont:[UIFont systemFontOfSize:17]];
    labelTitle.textAlignment = NSTextAlignmentCenter;//居中
    labelTitle.text = dictionary[@"newsTitle"];
    
    UILabel * labelContent = [[UILabel alloc] initWithFrame:CGRectZero];
//    labelContent.text = dictionary[@"newsContent"];
    labelContent.font = [UIFont systemFontOfSize:14];
//    CGSize size = [labelContent.text sizeWithFont:labelContent.font constrainedToSize:CGSizeMake(SCREEN_WIDTH-40, 10000) lineBreakMode:NSLineBreakByCharWrapping];
    //富文本处理
    NSString * htmlString = dictionary[@"newsContent"];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];//第一次解析
    NSString * string = [attrStr string];//转成string
    NSAttributedString * attrStrTemp = [[NSAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    NSString * stringOne = [attrStrTemp string];//转成string
    CGSize size = [stringOne sizeWithFont:labelContent.font constrainedToSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];//第二次解析
    labelContent.text = stringOne;//设置内容展示
    labelContent.numberOfLines = 0;
    labelContent.frame = CGRectMake(20, 80, size.width, size.height);//设置frame
    labelContent.textColor = [UIColor whiteColor];//设置字体颜色
    UIView * headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, size.height+100)];
    [headView addSubview:labelTitle];
    [headView addSubview:labelContent];
    [headView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setTableHeaderView:headView];
    
    [self.tableView setBackgroundColor:[UIColor clearColor]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ;
}

@end
